package use_case.handlereturns;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;

import services.interfaces.BillingServiceAdapter;
import services.interfaces.ServicesFactory;
import use_case.processRental.Customer;
import use_case.processRental.Kiosk;
import use_case.processRental.Ledger;
import use_case.processRental.Sale;

public class ReturnUI {
	private static final itemDescription NULL = null;
	private Kiosk Redbox;
	private JFrame frame, frmMainMenu;
	private Ledger kioskLedger;
	private JLabel lblOutput;
	private itemDescription d1; 
	Storage storage;
	Sale s1;
	Customer c1;
	private ArrayList<Customer> customerRecord = new ArrayList<Customer>();
	private ArrayList<Sale> saleRecord = new ArrayList<Sale>();
	private BillingServiceAdapter billingService;
	/**
	 * Create the application.
	 */
	public ReturnUI(JFrame frmMainMenu, Ledger kioskLedger) {
		this.kioskLedger = kioskLedger;
		this.frmMainMenu = frmMainMenu;
		initialize();
		display();
	
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		int width = 450;
		int height = 350;
		
		frame = new JFrame("Return Process");
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setBackground(new Color(180, 0, 0));
		frame.getContentPane().setLayout(null);
		
		storage = new Storage();
		storage.initialize();
		//Create Kiosk
		Redbox = new Kiosk(storage, kioskLedger);
		
		c1 = new Customer("Roger", "1");
		s1 = new Sale(storage);
		s1.addItem(0);
		customerRecord.add(c1);
		saleRecord.add(s1);
		Random generator = new Random(19580427);
		d1 = new DVD("A-Team", "Action", generator.nextInt(100000));
		
		lblOutput = new JLabel("Initializing Please Wait...");
		lblOutput.setForeground(Color.WHITE);
		lblOutput.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblOutput.setBounds(100, 80, 300, 30);
		//					x	y		
		frame.getContentPane().add(lblOutput);
		
		frame.setSize(width,height);
		frame.setVisible(true);
		
		
	}
	
	public void display(){
				
				lblOutput.setText("Insert Item");	
	             
	             if(Redbox.returnMode(d1)){
	            	 
	            	 Calendar returnDay = Calendar.getInstance();
	            	 saleRecord.get(0).setReturnDate(returnDay);
	            	 billingService = (BillingServiceAdapter) ServicesFactory.getInstance().getBillingServiceAdapter();
	            	 billingService.updateRentalRecord(customerRecord, saleRecord);
     			
	            	 lblOutput.setText("Successful Return...");
	             }
	             else{
	            	 lblOutput.setText("Unable to Return...");
	             }
       
	}

}
